package org.example.gateway.config.properties;

import cn.hutool.core.util.ObjectUtil;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.PathContainer;
import org.springframework.web.util.pattern.PathPattern;
import org.springframework.web.util.pattern.PathPatternParser;

import java.util.ArrayList;
import java.util.List;

/**
 * 放行白名单配置
 */
@Data
@NoArgsConstructor
@Configuration
@RefreshScope
@ConfigurationProperties(prefix = "security.ignore")
public class IgnoreWhiteProperties {
    /**
     * 放行白名单配置，网关不校验此处的白名单
     */
    private List<String> whites = new ArrayList<>();



    public boolean checkUrls(String pathUrl){
        if(ObjectUtil.isNotEmpty(whites)&&whites.size()>0){
            return whites.stream().anyMatch(url->{
                PathPattern pathPattern = PathPatternParser.defaultInstance.parse(url);
                PathContainer pathContainer = PathContainer.parsePath(pathUrl);
                return pathPattern.matches(pathContainer);
            });
        }
        return false;
    }

}
